// 参考博客地址：https://blog.csdn.net/qq_63358859/article/details/126665593?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-1-126665593-blog-118555676.pc_relevant_default&spm=1001.2101.3001.4242.2&utm_relevant_index=4
Component({
  options: {
    styleIsolation: "apply-shared",
  },
  properties: {
    show: {
      type: Boolean,
      value: false,
    },
    title: {
      type: String,
      value: "",
    },
    confirmText: {
      type: String,
      value: "确认",
    },
  },
  data: {
    hideModal: true, //模态框的状态 true-隐藏 false-显示
    animationData: {}, //
    useIpd: true,
  },
  observers: {
    show(newVal, oldVal) {
      if (newVal) {
        this.showModal();
      } else {
        this.hideModal();
      }
    },
  },
  attached() {
    const currRoute = getCurrentPages().slice(-1)[0].route;
    const hasTabbar = [
      "pages/home/home",
      "pages/love/love",
      "pages/mine/mine",
    ].includes(currRoute);
    this.setData({
      useIpd: !hasTabbar,
    });
  },
  methods: {
    onCatch() {
      return true;
    },
    showModal: function () {
      const that = this;
      that.setData({
        hideModal: false,
      });
      const animation = wx.createAnimation({
        duration: 300, //动画的持续时间 默认400ms 数值越大，动画越慢 数值越小，动画越快
        timingFunction: "ease", //动画的效果 默认值是linear
      });
      this.animation = animation;
      setTimeout(function () {
        that.fadeIn(); //调用显示动画
      }, 200);
    },

    // 隐藏遮罩层
    hideModal: function () {
      const that = this;
      const animation = wx.createAnimation({
        duration: 300, //动画的持续时间 默认400ms 数值越大，动画越慢 数值越小，动画越快
        timingFunction: "ease", //动画的效果 默认值是linear
      });
      this.animation = animation;
      that.fadeDown(); //调用隐藏动画
      setTimeout(function () {
        that.setData({
          hideModal: true,
        });
      }, 200); //先执行下滑动画，再隐藏模块
    },

    //动画集
    fadeIn: function () {
      this.animation.translateY(0).step();
      this.setData({
        animationData: this.animation.export(), //动画实例的export方法导出动画数据传递给组件的animation属性
      });
    },
    fadeDown: function () {
      this.animation.translateY(650).step();
      this.setData({
        animationData: this.animation.export(),
      });
    },
    onClose() {
      this.hideModal();
      this.triggerEvent("close");
    },
    onComfirm() {
      this.triggerEvent("confirm");
    },
  },
});
